//定义spu接口
import request from '@/utils/request'
import type {
  AllTradeMark,
  HasSaleAttrResponseData,
  HasSpuResponseData,
  SaleAttrResponseData,
  SkuData,
  SkuInfoData,
  SpuData,
  SpuHasImg,
} from './type'
enum API {
  //获取已有的spu数据
  HASSPU_URL = '/admin/product/{page}/{limit}',
  //获取全部品牌的数据
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  //获取某个spu售卖商品的图片数据
  IMAGE_URL = '/admin/product/spuImageList',
  //获取某个spu已有的销售属性的接口
  SPUHASSALEATTR_URL = '/admin/product/spuSaleAttrList/',
  //获取整个项目全部的销售属性
  ALLSALEATTR_URL = '/admin/product/baseSaleAttrList',
  //新增spu
  ADDSPU_URL = '/admin/product/saveSpuInfo',
  //修改已有的spu
  UPDATESPU_URL = '/admin/product/saveSpuInfo,',
  //添加sku
  ADDSKU_URL = 'admin/product/saveSkuInfo',
  //查看某一个spu下全部售卖的商品sku
  SKUINFO_URL = 'admin/product/findBySpuId/{spuId}',
  //删除已有的SPU
  REMOVESPU_URL = '/admin/product/deleteSpu/',
}

//获取spu数据的接口方法
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: number | string,
) => {
  request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )
}
//获取全部品牌数据的接口方法
export const reqAllTradeMark = () => {
  request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL)
}
//获取某个spu商品的图片数据
export const reqSpuImageList = (spuId: number) => {
  request.get<any, SpuHasImg>(API.IMAGE_URL + spuId)
}
//获取某个spu已有的销售属性的接口方法
export const reqSpuHasSaleAttr = (spuId: number) => {
  request.get<any, SaleAttrResponseData>(API.SPUHASSALEATTR_URL + spuId)
}
//获取全部的销售属性接口方法
export const reqAllSaleAttr = () => {
  request.get<any, HasSaleAttrResponseData>(API.ALLSALEATTR_URL)
}
//添加和修改spu接口方法
export const reqAddOrUpdateSpu = (data: SpuData) => {
  //如果spu对象有id，则发送更新请求
  if (data.id) {
    return request.post<any, any>(API.UPDATESPU_URL, data)
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data)
  }
}
//添加sku接口方法
export const reqAddSku = (data: any) => {
  request.post<any, SkuData>(API.ADDSKU_URL, data)
}
//获取sku接口方法
export const reqSkuInfo = (spuId: number | string) => {
  request.get<any, SkuInfoData>(API.SKUINFO_URL + spuId)
}
//删除已有的spu方法
export const reqRemoveSpu = (spuId: number | string) => {
  request.delete<any, any>(API.REMOVESPU_URL + spuId)
}
